package com.ctcemti.mdm.common.util;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @ClassName MybatisUtils
 * @Description mybatis获取connection
 * @Author bxf
 * @Date 9:20 2019/7/25
 * @Version V1.0
 **/
@Component
public class MybatisUtils {
    @Resource
    protected SqlSessionTemplate sqlSessionTemplate;
    /**
     * 获取原生SqlSession,需要手动关闭SqlSession
     * 对应 {@code BaseDaoImpl.closeNativeSqlSession}
     * @return
     */
    public SqlSession getNativeSqlSession() {
        return SqlSessionUtils.getSqlSession(sqlSessionTemplate.getSqlSessionFactory(),
                sqlSessionTemplate.getExecutorType(),sqlSessionTemplate.getPersistenceExceptionTranslator());
    }
    /**
     * 关闭原生SqlSession
     * 对应 getNativeSqlSession
     * @return
     */
    public void closeNativeSqlSession(SqlSession sqlSession){
        SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionTemplate.getSqlSessionFactory());
    }

}
